﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Web;
using ElectronicCommerce.Framework.Payment.Contract.DataContract;
using ElectronicCommerce.Framework.Payment.Contract.ServiceContract;
using ElectronicCommerce.Framework.Payment.Utility;

namespace ElectronicCommerce.Framework.Payment.API
{
    /// <summary>
    /// 
    /// </summary>
    public class PaymentServiceClient : ClientBase<IPaymentService>, IPaymentService
    {

        public PaymentServiceClient()
        {
        }

        public PaymentServiceClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
        }

        public PaymentServiceClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

        public PaymentServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

        public PaymentServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
        }

        public ChargeRequest Charge(ChargeRequest chargeRequest)
        {
            ChargeRequest innerChargeRequest = base.Channel.Charge(chargeRequest);
            if (innerChargeRequest.Gateway.RequestType == GatewayRequestType.HttpGet)
            {
                HttpContext.Current.Response.Redirect(innerChargeRequest.RequestText, true);
            }
            else if (innerChargeRequest.Gateway.RequestType == GatewayRequestType.HttpPost)
            {
                FormHelper.SubmitForm(innerChargeRequest.RequestText);
            }

            return innerChargeRequest;
        }

        public ChargeCallbackResult ChargeCallback(ChargeCallbackRequest chargeCallbackRequest)
        {
            return base.Channel.ChargeCallback(chargeCallbackRequest);
        }

        public void NotifyGatewayChargeResult(ChargeCallbackResult callbackResult)
        {
            base.Channel.NotifyGatewayChargeResult(callbackResult);
        }

        public RefundResult Refund(RefundRequest refundRequest)
        {
            return base.Channel.Refund(refundRequest);
        }
    }
}
